<?php
$JO[ "TB"		]	= JO( array( "EN" => "SMS Statistics",	"VI" => "Bảng Thống Kê Tin Nhắn"));
$JO[ "Fromdate"	] 	= JO( array( "EN" => "From Date",		"VI" => "Từ Ngày"));
$JO[ "Todate"	]	= JO( array( "EN" => "To Date",			"VI" => "Đến Ngày"));
$JO[ "Ma"		]	= JO( array( "EN" => "Code",			"VI" => "Mã Tin Nhắn"));
$JO[ "ND"		]	= JO( array( "EN" => "Content",			"VI" => "Nội Dung"));
$JO[ "SL"		]	= JO( array( "EN" => "Sum",				"VI" => "Số Lượng"));
$JO[ "Xem"		]	= JO( array( "EN" => "View",			"VI" => "Xem"));
$JO[ "syntax"	]	= JO( array( "EN" => "Syntax",			"VI" => "Cú Pháp"));
$JO[ "number"	]	= JO( array( "EN" => "Number",			"VI" => "Đầu Số"));
$JO[ "all"		]	= JO( array( "EN" => "All",				"VI" => "Tất Cả"));
?>
<script type="text/javascript">
                $(document).ready(function() {
                    $(".datepicker").datepicker({
                        //changeYear:true

                    });

                    $(".datepicker").mask("99/99/9999", { placeholder: "_" });

                });

                </script>

<div class="JO_left">
	<!-- Form -->

        <div class="JO_bar_720">
            <h1> <?=$JO["TB"]?></h1>
            <form
                name="frm" id="frm"
                method="post"
                action="JO_00_integration.php?SO_unit=24_SMS_statistic"
            >

		        <?php
		        $fromdate=$_REQUEST["Fromdate"];
		        $todate=$_REQUEST["Todate"];
		         if($fromdate!="" && $fromdate!=null){
		                 $DK_Ngay.=" and to_days('".substr($fromdate,6,4)."-".substr($fromdate,0,2)."-".substr($fromdate,3,2)."')<= to_days(`sms`.`requesttime`)";
		             }
		         if($todate!="" &&  $todate!=null){
		                 $DK_Ngay.=" and to_days('".substr($todate,6,4)."-".substr($todate,0,2)."-".substr($todate,3,2)."')>= to_days(`sms`.`requesttime`)";
		          }
		        ?>
                <table style="width: 100%">
                <tr>
                	<td><?=$JO["syntax"];?></td>
                    <td style="width: 140px">
                        <select id="cmb_syntax"
                        		name="cmb_syntax"
                        		style="width:100%;padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px;height: auto;"
                        >
                        	<option value="HT" 
                        	<?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="HT")
                        		echo "selected=\"selected\"";
                        	?>
                        	>HT</option>
                        	<option value="HQ"
                        	<?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="HQ")
                        		echo "selected=\"selected\"";
                        	?>
                        	>HQ</option>
                        	<option value="NP"
                        	<?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="NP")
                        		echo "selected=\"selected\"";
                        	?>
                        	>NP</option>
                        </select>
                    </td>
                    <td><?=$JO["number"];?></td>
                    <td style="width: 140px">
                    	<select id="cmb_number"
                    			name="cmb_number"
                    			style="width:100%;padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px;height: auto;"
                    	>
                    		<option value="all"
                    		<?php if(isset($_POST["cmb_number"])&& $_POST["cmb_number"]=="all")
                        		echo "selected=\"selected\"";
                        	?>
                    		><?=$JO["all"];?></option>
                        	<option value="8588"
                        	<?php if(isset($_POST["cmb_number"])&& $_POST["cmb_number"]=="8588")
                        		echo "selected=\"selected\"";
                        	?>
                        	>8588</option>
                        	<option value="8388"
                        	<?php if(isset($_POST["cmb_number"])&&$_POST["cmb_number"]=="8388")
                        		echo "selected=\"selected\"";
                        	?>
                        	>8388</option>
                        	<option value="8288"
                        	<?php if(isset($_POST["cmb_number"])&&$_POST["cmb_number"]=="8288")
                        		echo "selected=\"selected\"";
                        	?>
                        	>8288</option>
                        	<option value="8188"
                        	<?php if(isset($_POST["cmb_number"])&&$_POST["cmb_number"]=="8188")
                        		echo "selected=\"selected\"";
                        	?>
                        	>8188</option>
                        	<option value="8088"
                        	<?php if(isset($_POST["cmb_number"])&&$_POST["cmb_number"]=="8088")
                        		echo "selected=\"selected\"";
                        	?>
                        	>8088</option>
                        </select>
                    </td>
                	<td></td>
                </tr>
                <tr>
                    <td > <?=$JO["Fromdate"]?> </td>
                    <td >
                        <input id="Fromdate" name="Fromdate"
                               style="width:140px;padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px;height: auto;"
                               class="datepicker"
                               value="<?=$fromdate?>"/>
                    </td>
                    <td>
                        <?=$JO["Todate"]?>
                    </td>
                    <td >
                        <input id="Todate" name="Todate" type="text"
                               style="width:140px;padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px;height: auto;"
                               class="datepicker"
                               value="<?=$todate?>"/>
                    </td>

                    <td >
                        <input type="submit" name="Xem" class="JO_button" value="<?=$JO["Xem"]?>"
                               style="width: 100px;padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px;height: auto;text-align: center;"                              
                               />
                    </td>
            </tr>
           </table>
           </form>
            <table style="width: 100%">
            <tr >
                    <td ><?=$JO["Ma"];		?></td>
                    <?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="HT"){?>
                    <td>phone</td>
                    <?php }?>
                    <td ><?=$JO["ND"];		?></td>
                    <td ><?=$JO["number"];	?></td>
                    <td ><?=$JO["SL"];		?></td>
                    
                </tr>

                <?php
                if(isset ($_POST["Xem"])){
                	$query_TK="";
                	$s_where="";
                	$s_groupby="";
                	if($_POST["cmb_syntax"]=="HT"){
                    	$query_TK="select `sms`.`src`,`sms`.`dest`,`sms`.`cmdcode`,`sms`.`msgbody`,count(`cmdcode`) as SoLuong from `sms` where `sms`.`cmdcode`= 'HT'".$DK_Ngay;
                	}
                   	elseif($_POST["cmb_syntax"]=="HQ"){
                    	$query_TK="
                    		select `sms`.`src`,`sms`.`cmdcode`,substring(`sms`.`msgbody`,1,4) as msgbref,count(`cmdcode`) as SoLuong
                    		from `sms`
                    		where `sms`.`cmdcode`= 'HQ'".$DK_Ngay;
                    }
                    elseif($_POST["cmb_syntax"]=="NP"){
                    	$query_TK="select `sms`.`src`,`sms`.`cmdcode`,`sms`.`msgbody`,count(`cmdcode`) as SoLuong from `sms` where `sms`.`cmdcode`= 'NP'".$DK_Ngay;
                    }
                    
                    if($_POST["cmb_number"]!="all"){
                    	$query_TK .= " AND `src`=".$_POST["cmb_number"];
                    }
                    
                    if($_POST["cmb_syntax"]=="HQ"){
                    	$s_groupby = " group by `msgbref`";
                    }elseif ($_POST["cmb_syntax"]=="HT"){
                    	$s_groupby = " group by `dest`";
                    }else 
                    	$s_groupby = " group by `msgbody`";
                    	
                    $query_TK .=$s_groupby;
                    //echo $query_TK;
                    $recordSet = $JO_MySQL -> query( $query_TK );
                    $i_total = 0;
                	while($record = $recordSet -> fetch_array()) {
                ?>
                <tr>
                    <td><?php echo $record["cmdcode"] ?></td>
                    <?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="HT"){?>
                    <td><?php echo $record["dest"] ?></td>
                    <?php }?>
                    <td>
                    	<?php
                    	if($record["msgbody"]!=null)
                    		echo $record["msgbody"];
                    	else echo $record["msgbref"];
                    	?>
                    </td>
                    <td><?php echo $record["src"]?></td>
                    <td><?php echo $record["SoLuong"]?></td>

                </tr>
                <?php
                		$i_total += $record["SoLuong"];
                	}?>
                <tr>
                	<td>Tổng lượt nhắn tin</td>
                	<td></td>
                	<td></td>
                	 <?php if(isset($_POST["cmb_syntax"])&& $_POST["cmb_syntax"]=="HT"){?>
                    <td></td>
                    <?php }?>
                	<td><?=$i_total;?></td>
                </tr>
                <?php 
                }
                ?>

            </table>
        </div>

</div>

                             
<!--                /*
                 * if(ngay bat dau khong trong va ngay ket thuc khong trong){
                 * $query="select * from sms + dieu kien ngay bat dau + dk ngay ket thuc";
                 * }else if(neu ngay bat dau khong trong){
                 * $query="select * from sms + dieu kien ngay bat dau
                 * }else if(neu ngay ket thuc khong trong){
                 * $query="select * from sms + dk ngay ket thuc";
                 * }else{
                 * $query="select * from sms";
                 * }
                 */
                //echo $DK_Ngay;
                -->
